package thread_test;

public class ThreadDemo14 {
    public static void main(String[] args) {
        Object locker = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            int n = 0;
            while (n < 10){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker) {
                    System.out.print("A");
                    n++;
                    locker.notify();
                }
            }
        },"c");
        Thread t2 = new Thread(()->{
            int n = 0;
            while (n < 10){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker) {
                    synchronized (locker2) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.print("B");
                        n++;
                        locker2.notify();
                    }
                }
            }
        },"b");
        Thread t3 = new Thread(()->{
            int n = 0;
            while (n < 10){
                synchronized (locker2) {
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("C");
                    n++;
                }
            }
        },"a");
        t1.start();
        t2.start();
        t3.start();
    }
}
